<?php

namespace Mlym\CodeGeneration\ControllerGeneration\Method;


use EasySwoole\ORM\Utility\Schema\Column;
use Mlym\CodeGeneration\Middleware\Bean\View\ViewColumnBean;

class Del extends MethodAbstract
{

    protected $methodName = 'del';
    protected $methodDescription = '删除数据';
    protected $methodAllow = 'GET';

    function addMethodBody()
    {
        $method = $this->method;

        $modelName = $this->getModelName();
        $table = $this->controllerConfig->getTable();
        $primaryKey = $table->getPkFiledName() ?? '';
        $this->chunkSchemaColumn(function (ViewColumnBean $column) use (&$methodBody) {
            $param = $this->buildValidateColumnParam($column);
            $isPrimary = $this->controllerConfig->getTable()->getTableName() == $column->getModule() && $this->controllerConfig->getTable()->getPkFiledName() == $column->getName();
            if ($isPrimary) {
                $param->required = '';
                $this->addColumnComment($param);
                return true;
            }
            return false;
        });

        $methodBody = <<<Body
\$param = ContextManager::getInstance()->get('param');
\$model = new {$modelName}();
\$ids = explode(',',\$param['{$primaryKey}']?? '');
empty(\$ids) || \$model->destroy(\$ids);
\$this->writeJson(Status::CODE_OK, [], "删除成功.");
Body;
        $method->setBody($methodBody);

    }


    function addComment()
    {
        $this->method->addComment("@throws \Throwable");
    }
}
